package com.weitu.permission.roleGroup.controller;

import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.roleGroup.entity.RoleGroup;
import com.weitu.permission.roleGroup.service.RoleGroupService;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/3/17.
 */
@Controller
@RequestMapping(value = "permission/roleGroup")
public class RoleGroupController extends BaseController{
    private static final String MODULE_BATH_PATH=BASE_JSP_PATH+"permission/roleGroup";
    private final RoleGroupService roleGroupService;

    @Autowired
    public RoleGroupController(RoleGroupService roleGroupService){
        this.roleGroupService=roleGroupService;
    }

    /**
     * 跳转到角色组页面
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String list(){
        return MODULE_BATH_PATH+"/list";
    }

    /**
     * 分页查询角色组列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Object listAjax(HttpServletRequest request){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        putParametersIntoMap(paramMap);

        int count=this.roleGroupService.queryCount(paramMap);

        DataGridPagination dataGridPagination=DataGridPagination.getInstance(request,count);

        PageView pageView=dataGridPagination.getPageView();

        paramMap.put("pageView",pageView);

        List<RoleGroup> dataList=this.roleGroupService.queryRoleGroupList(paramMap);

        return dataGridPagination.flushDataGridData(dataList);
    }

    @ResponseBody
    @RequestMapping(value = "/listAll",method = RequestMethod.POST)
    public Object listAll(){
        List<RoleGroup> dataList=this.roleGroupService.queryRoleGroupListAll();
        return dataList;
    }

    /**
     * 弹出角色组添加弹框
     * @return
     */
    @RequestMapping(value = "addRoleGroup",method = RequestMethod.GET)
    public String addRoleGroup(){
        return MODULE_BATH_PATH+"/add";
    }

    /**
     * 角色组添加
     * @param roleGroup
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addRoleGroup",method = RequestMethod.POST)
    @SystemLog(module = "角色组管理",methods = "添加角色组")
    public AjaxResult addRoleGroupAjax(RoleGroup roleGroup){
        Map<String, Object> paramMap = new HashMap<String, Object>();
        AjaxResult result = new AjaxResult();

        putParametersIntoMap(paramMap);

        roleGroup.setId(IDGenerator.getId());
        roleGroup.setAddTime(DateTimeUtil.formatterDateTime());
        roleGroup.setAddUserId(BaseController.getSessionUser().getId());
        roleGroup.setDelState(0);
        int count=this.roleGroupService.insertRoleGroup(roleGroup);
        if (count>0){
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("保存失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 弹出编辑弹出框
     * @return
     */
    @RequestMapping(value = "editRoleGroup",method = RequestMethod.GET)
    public ModelAndView editRoleGroup(String id){
        RoleGroup roleGroup=this.roleGroupService.queryRoleGroupById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("roleGroup", roleGroup);
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        return modelAndView;
    }

    /**
     * 编辑角色组
     * @param roleGroup
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "editRoleGroup",method = RequestMethod.POST)
    @SystemLog(module = "角色组管理",methods = "编辑角色组")
    public AjaxResult editRoleGroupAjax(RoleGroup roleGroup){
        roleGroup.setUpdateTime(DateTimeUtil.formatterDateTime());
        roleGroup.setUpdateUserId(BaseController.getSessionUser().getId());
        roleGroup.setDelState(0);
        int count=this.roleGroupService.updateRoleGroup(roleGroup);
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("编辑成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("编辑失败！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 删除角色组
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteRoleGroup",method = RequestMethod.POST)
    @SystemLog(module = "角色组管理",methods = "删除角色组")
    public AjaxResult deleteRoleGroupAjax(String id){
        int count=this.roleGroupService.deleteRoleGroup(id);
        AjaxResult result=new AjaxResult();
        if (count>0){
            result.setMessage("删除成功！");
            result.setSuccess(true);
            return result;
        }else {
            result.setMessage("删除失败！");
            result.setSuccess(false);
            return result;
        }
    }
    /**
     * 添加编辑判断code
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addAndAlterRoleGroupJudgement",method = RequestMethod.POST)
    public AjaxResult addAndAlterRoleGroupJudgement(String code){
        int count=this.roleGroupService.queryCountByCode(code);
        AjaxResult result = new AjaxResult();
        if (count>0){
            result.setMessage("标识重复，保存失败！");
            result.setSuccess(false);
            return result;
        }else {
            result.setMessage("保存成功！");
            result.setSuccess(true);
            return result;
        }
    }
}
